const fs = require('fs');
const path = require('path');


const http = require('http');
let server = http.createServer()
server.listen(3000, () => {
    console.log("3000监听成功");
})


// 根据用户请求的不同路径，响应不同的内容
server.on("request", (req, res) => {


    if (req.url == "/favicon.ico") {
        res.end() //停止这次请求
        return //停止这个函数
    }


    let { pathname } = new URL(req.url, "http://localhost:3000/")


    if (pathname == "/index") {

        // 拼接路径
        let filepath = path.join(__dirname, "../html/index.html")

        // 读取文件
        let result = fs.readFileSync(filepath)

        res.end(result)
    } else
        if (pathname == "/list") {
            // 拼接路径
            let filepath = path.join(__dirname, "../html/list.html")

            // 读取文件
            let result = fs.readFileSync(filepath)

            res.end(result)
        } else
            if (pathname == "/detail") {
                // 拼接路径
                let filepath = path.join(__dirname, "../html/detail.html")

                // 读取文件
                let result = fs.readFileSync(filepath)

                res.end(result)
            } else {


                // 以上路径都没有匹配成功

                // 拼接路径
                let filepath = path.join(__dirname, "../html/404.html")

                // 读取文件
                let result = fs.readFileSync(filepath)



                res.writeHead(404,{"Content-type":"text/html;charset=utf-8"})
                res.end(result)

            }




})